package com.example.demo.controller;

import com.example.demo.entity.ShippingAddress;
import com.example.demo.service.ShippingAddressService;
import com.example.demo.vo.Result;
import com.example.demo.vo.ResultCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: TODO
 * @Author Mr.Tang
 */
@Tag(name = "收货地址接口")
@Slf4j
@RestController
@RequestMapping("/address")
public class AddressController{

    @Resource
    private ShippingAddressService addressService;


    @Operation(summary = "根据收货地址id查询一条收货地址")
    @GetMapping("/selectAddressById")
    public Result<ShippingAddress> selectAddressById(@Parameter(name = "id", description = "收货地址id", required = true) @NonNull Long id) {
        // 调用service
        ShippingAddress address = addressService.selectAddressById(id);

        // 成功返回
        return Result.ok(address);
    }

    @Operation(summary = "根据用户id拿到其所有收货地址")
    @GetMapping("/selectAllAddressByUid")
    public Result<List<ShippingAddress>> selectAllAddressByUid(@Parameter(name = "id", description = "用户id", required = true) @NonNull Long id) {
        // 调用service
        List<ShippingAddress> addresses = addressService.selectAllAddressByUid(id);

        // 成功返回
        return Result.ok(addresses);
    }

    @Operation(summary = "添加收获地址")
    @PostMapping("/addShippingAddress")
    public Result addShippingAddress(@Parameter(name = "id", description = "用户id", required = true) @NonNull Long uid,
                                     @Parameter(name = "nickname", description = "用户昵称") String nickname,
                                     @Parameter(name = "phone", description = "电话号码") String phone,
                                     @Parameter(name = "shippingAddress", description = "收货地址") String shippingAddress) {
        // 1. 构造一个收货地址对象
        ShippingAddress address = new ShippingAddress();
        address.setUid(uid);
        address.setNickname(nickname);
        address.setPhone(phone);
        address.setAddress(shippingAddress);

        // 2. 调用service
        addressService.addShippingAddress(address);

        // 3. 成功返回
        return Result.ok(ResultCode.SUCCESS);
    }


    @Operation(summary = "修改收货地址")
    @PostMapping("/modifyShippingAddress")
    public Result modifyShippingAddress(@Parameter(name = "id", description = "收货地址id", required = true) @NonNull Long id,
                                        @Parameter(name = "nickname", description = "用户昵称") String nickname,
                                        @Parameter(name = "phone", description = "电话号码") String phone,
                                        @Parameter(name = "shippingAddress", description = "收货地址") String shippingAddress) {
        // 1. 构造一个收货地址对象
        ShippingAddress address = new ShippingAddress();
        address.setId(id);
        address.setNickname(nickname);
        address.setPhone(phone);
        address.setAddress(shippingAddress);

        // 2. 调用service
        addressService.modifyShippingAddress(address);

        // 3. 成功返回
        return Result.ok(ResultCode.SUCCESS);
    }


    @Operation(summary = "根据id删除一条收货地址数据")
    @DeleteMapping("/deleteAddressById")
    public Result deleteAddressById(@Parameter(name = "id", description = "收货地址id", required = true) @NonNull Long id) {
        // 1. 调用service
        addressService.deleteAddressById(id);
        // 2. 成功返回
        return Result.ok(ResultCode.SUCCESS);
    }

    @Operation(summary = "改变被选中状态")
    @PostMapping("/updateSelectedById")
    public Result updateSelectedById(@Parameter(name = "id", description = "收货地址id", required = true) @NonNull Long id,
                                   @Parameter(name = "uid", description = "用户id", required = true) @NonNull Long uid){
        addressService.updateSelectedById(id,uid);
        return Result.ok(ResultCode.SUCCESS);
    }

    @Operation(summary = "查询默认收获地址的地址id")
    @GetMapping("/getIdBySelected")
    public Long getIDBySelected(@Parameter(name = "uid", description = "用户id", required = true) @NonNull Long uid){
        if( addressService.getIDBySelected(uid) == null ) {
            return -1L;
        }
        return addressService.getIDBySelected(uid);
    }

}
